<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="medium"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入题干内容/知识点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['question:subjective:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['question:subjective:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['question:objective:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['question:subjective:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="6">
        <div class="inner-select">
          <span>题目类型：</span>
          <el-select
            v-model="queryParams.quType"
            placeholder="请选择"
            size="small"
            @change="changeQuType"
          >
            <el-option
              v-for="dict in dict.type.sub_qu_type"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="inner-select">
          <span>难度等级：</span>
          <el-select
            v-model="queryParams.quGrand"
            placeholder="请选择"
            size="small"
            @change="changeQuGrand"
            :clearable="true"
          >
            <el-option
              v-for="dict in dict.type.qu_qu_grand"
              :value="dict.value"
              :label="dict.label"
            ></el-option>
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subjectiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题干内容" align="center" prop="subContent" :show-overflow-tooltip="true" />
      <el-table-column label="题目类型" align="center" prop="subType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sub_qu_type" :value="scope.row.subType" />
        </template>
      </el-table-column>
      <el-table-column label="难度等级" align="center" prop="subGrand">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.subGrand" disabled></el-rate>
        </template>
      </el-table-column>
      <el-table-column
        label="知识点"
        align="center"
        prop="subTag"
        width="180px"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <my-tag :valStr="scope.row.subTag" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['question:subjective:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['question:subjective:remove']"
          >删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleInfo(scope.row)">详细信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主观题对话框 -->
    <sub-up-form :title="title" :visible.sync="open" :subInfo="form" />

    <!-- 弹出详细信息弹窗 -->
    <sub-info :visible.sync="infoDialog" :subInfo="subInfo" />

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate"
          >下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuestion,
  getQuestion,
  delQuesion
} from "@/api/question/objAndSub";
import SubInfo from "@/components/QuInfo/subInfo";
import { getToken } from "@/utils/auth";
import SubUpForm from "@/components/Question/subUpForm";
export default {
  name: "Subjective",
  dicts: ["sub_qu_type", "qu_qu_grand"],
  components: {
    SubInfo,
    SubUpForm
  },
  data() {
    return {
      //弹出数据信息
      subInfo: null,
      // 详细信息弹框
      infoDialog: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主观题表格数据
      subjectiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        quType: 4,
        quGrand: undefined
      },
      // 表单参数
      form: {},
      // 客观题导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/question/question/importSub"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询主观题列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.subjectiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.quGrand = undefined;
      this.queryParams.quType = 4;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = null;
      this.open = true;
      this.title = "添加主观题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = null;
      getQuestion({ quId: row.subId, quType: row.subType }).then(res => {
        this.form = res.data;
        this.open = true;
        this.title = "修改主观题目";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const subIds = row.subId || this.ids;
      this.$modal
        .confirm('是否确认删除主观题编号为"' + subIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delQuesion(subIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "question/question/export",
        {
          ...this.queryParams
        },
        `主观题列表_${new Date().getTime()}.xlsx`
      );
    },
    /** 更改题目类型 */
    changeQuType() {
      this.getList();
    },
    /** 更改题目难度等级 */
    changeQuGrand() {
      this.getList();
    },
    /** 详细信息 */
    handleInfo(row) {
      this.infoDialog = true;
      this.subInfo = row;
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "客观题导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "question/question/quTemplate/2",
        {},
        `sub_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style scoped>
.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}
</style>